package com.jsoft.android.as;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class WF extends Activity {
	
	boolean mExternalStorageAvailable = false;
	boolean mExternalStorageWriteable = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String buf = "nothing passed in";
        String fn = "empty.txt";
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
                buf = extras.getString("buf");  // ExtraKey
                Log.d("got to write", buf);
        		fn = getIntent().getData().toString();
                Log.d("writing to", fn);


        }
        
        /*
         * if (extras != null) {
            String resultName = extras.getString("fileContent");  // ResultName from App Inventor
            Log.d("extras in we got", resultName);
            //if (resultName != null) {
            	//
            	//bundle.putCharSequence("fileContent", resultName);          	
            //}
        }
         */
        
        OutputFile(fn, buf);
        
        Bundle bundle = new Bundle();
            	bundle.putCharSequence("APP_INVENTOR_RESULT", "wrote to file");
           	    Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            
        
    }
    
    private  void OutputFile(String fileName, String s) {
            try {    
            	

            	/*
            	 * Make sure we have something we can use
            	 * http://developer.android.com/guide/topics/data/data-storage.html
            	 */
            	String state = Environment.getExternalStorageState();

            	if (Environment.MEDIA_MOUNTED.equals(state)) {
            	    // We can read and write the media
            	    mExternalStorageAvailable = mExternalStorageWriteable = true;
            	} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            	    // We can only read the media
            	    mExternalStorageAvailable = true;
            	    mExternalStorageWriteable = false;
            	} else {
            	    // Something else is wrong. It may be one of many other states, but all we need
            	    //  to know is we can neither read nor write
            	    mExternalStorageAvailable = mExternalStorageWriteable = false;
            	}
            	
            	// and If you're using API Level 8 or greater, use getExternalFilesDir() 
            	if (mExternalStorageAvailable && mExternalStorageWriteable) {
            		File sdCard = Environment.getExternalStorageDirectory();
            		Log.d("absolute path to file", sdCard.getAbsolutePath());
                    //File dir = new File (sdCard.getAbsolutePath() + "/test");
                    //dir.mkdirs();
                    File file = new File(sdCard.getAbsolutePath()+"/"+fileName);
                    Log.d("writing to file", sdCard.getAbsolutePath()+"/"+fileName);

                    FileOutputStream f = new FileOutputStream(file, true);
                    Log.d("writing", s);
                    f.write(s.getBytes());
                    f.close();
            	}
                } catch (Exception e) {
                    Log.d("OutputFile", e.getMessage());
                }
            	}
    
    

                

        }
